﻿Imports System.Data.SqlClient

Public Class NhaBUS

    Public Shared Function GetAll() As DataTable
        Dim listNha As New DataTable()

        listNha = NhaDAO.GetAll()

        Return listNha
    End Function
    Public Shared Function GetAllNhaTamXoa() As DataTable
        Dim listNha As New DataTable()

        listNha = NhaDAO.GetAllNhaTamXoa()

        Return listNha
    End Function
    Public Shared Function ExportDS_Nha() As DataTable
        Dim listNha As New DataTable()

        listNha = NhaDAO.ExportDS_Nha()

        Return listNha
    End Function

    Public Shared Function LayDSMaNha() As DataTable
        Dim dt As DataTable = New DataTable()
        dt = NhaDAO.LayDSMaNha()
        Return dt
    End Function

    Public Shared Sub Insert(ByVal nha As NhaDTO)
        Dim parameters(11) As SqlParameter

        Dim para As SqlParameter = New SqlParameter("MaNha", SqlDbType.VarChar)
        para.Direction = ParameterDirection.Input
        para.Value = nha.strMaNha
        parameters(0) = para

        para = New SqlParameter("SoNha", SqlDbType.VarChar)
        para.Direction = ParameterDirection.Input
        para.Value = nha.strSoNha
        parameters(1) = para

        para = New SqlParameter("Duong", SqlDbType.NVarChar)
        para.Direction = ParameterDirection.Input
        para.Value = nha.strDuong
        parameters(2) = para

        para = New SqlParameter("DienTich", SqlDbType.Int)
        para.Direction = ParameterDirection.Input
        para.Value = nha.iDienTich
        parameters(3) = para

        para = New SqlParameter("Gia", SqlDbType.Int)
        para.Direction = ParameterDirection.Input
        para.Value = nha.iGia
        parameters(4) = para

        para = New SqlParameter("TinhTrangNha", SqlDbType.Bit)
        para.Direction = ParameterDirection.Input
        para.Value = nha.isTinhTrang
        parameters(5) = para

        para = New SqlParameter("MaLoaiNha", SqlDbType.Int)
        para.Direction = ParameterDirection.Input
        para.Value = nha.iMaLoaiNha
        parameters(6) = para

        para = New SqlParameter("MaQuan", SqlDbType.Int)
        para.Direction = ParameterDirection.Input
        para.Value = nha.iMaQuan
        parameters(7) = para

        para = New SqlParameter("TamXoa", SqlDbType.Bit)
        para.Direction = ParameterDirection.Input
        para.Value = nha.isTamXoa
        parameters(8) = para

        para = New SqlParameter("MaNguoiDang", SqlDbType.VarChar)
        para.Direction = ParameterDirection.Input
        para.Value = nha.strMaNguoiDang
        parameters(9) = para

        para = New SqlParameter("TieuDe", SqlDbType.NVarChar)
        para.Direction = ParameterDirection.Input
        para.Value = nha.strTieuDe
        parameters(10) = para

        para = New SqlParameter("NgayDang", SqlDbType.Date)
        para.Direction = ParameterDirection.Input
        para.Value = nha.dNgayDang
        parameters(11) = para

        NhaDAO.Insert(parameters)
    End Sub

    Public Shared Sub Delete(ByVal paraID As SqlParameter)
        NhaDAO.Delete(paraID)
    End Sub

    Public Shared Sub Update(ByVal nha As NhaDTO)
        Dim parameters(11) As SqlParameter

        Dim para As SqlParameter = New SqlParameter("MaNha", SqlDbType.VarChar)
        para.Direction = ParameterDirection.Input
        para.Value = nha.strMaNha
        parameters(0) = para

        para = New SqlParameter("SoNha", SqlDbType.VarChar)
        para.Direction = ParameterDirection.Input
        para.Value = nha.strSoNha
        parameters(1) = para

        para = New SqlParameter("Duong", SqlDbType.NVarChar)
        para.Direction = ParameterDirection.Input
        para.Value = nha.strDuong
        parameters(2) = para

        para = New SqlParameter("DienTich", SqlDbType.Int)
        para.Direction = ParameterDirection.Input
        para.Value = nha.iDienTich
        parameters(3) = para

        para = New SqlParameter("Gia", SqlDbType.Int)
        para.Direction = ParameterDirection.Input
        para.Value = nha.iGia
        parameters(4) = para

        para = New SqlParameter("TinhTrangNha", SqlDbType.Bit)
        para.Direction = ParameterDirection.Input
        para.Value = nha.isTinhTrang
        parameters(5) = para

        para = New SqlParameter("MaLoaiNha", SqlDbType.Int)
        para.Direction = ParameterDirection.Input
        para.Value = nha.iMaLoaiNha
        parameters(6) = para

        para = New SqlParameter("MaQuan", SqlDbType.Int)
        para.Direction = ParameterDirection.Input
        para.Value = nha.iMaQuan
        parameters(7) = para

        para = New SqlParameter("TamXoa", SqlDbType.Bit)
        para.Direction = ParameterDirection.Input
        para.Value = nha.isTamXoa
        parameters(8) = para

        para = New SqlParameter("MaNguoiDang", SqlDbType.VarChar)
        para.Direction = ParameterDirection.Input
        para.Value = nha.strMaNguoiDang
        parameters(9) = para

        para = New SqlParameter("TieuDe", SqlDbType.NVarChar)
        para.Direction = ParameterDirection.Input
        para.Value = nha.strTieuDe
        parameters(10) = para

        para = New SqlParameter("NgayDang", SqlDbType.Date)
        para.Direction = ParameterDirection.Input
        para.Value = nha.dNgayDang
        parameters(11) = para

        NhaDAO.Update(parameters)
    End Sub

    Public Shared Sub Update_TamXoa(ByVal manha As String, ByVal istamxoa As Boolean)
        Dim parameters(1) As SqlParameter

        Dim para As SqlParameter = New SqlParameter("MaNha", SqlDbType.VarChar)
        para.Direction = ParameterDirection.Input
        para.Value = manha
        parameters(0) = para

        para = New SqlParameter("TamXoa", SqlDbType.Bit)
        para.Direction = ParameterDirection.Input
        para.Value = istamxoa
        parameters(1) = para

        NhaDAO.Update_TamXoa(parameters)
    End Sub

    Public Shared Function LayMa_Nha_Cuoi() As String
        Dim macuoi As String
        macuoi = NhaDAO.LayMa_Nha_Cuoi()
        Return macuoi
    End Function

    Public Shared Function TinhMaNha(ByVal macuoi As String) As String
        Dim manha As String = ""
        Dim num As Integer = macuoi.Substring(2) + 1
        If num < 10 Then
            manha = "NH000" & num
        ElseIf num >= 10 And num < 100 Then
            manha = "NH00" & num
        ElseIf num >= 100 And num < 1000 Then
            manha = "NH0" & num
        ElseIf num >= 1000 Then
            manha = "NH" & num
        End If

        Return manha
    End Function

    Public Shared Function TimKiemNhaTheoQuan(ByVal maquan As Integer) As DataTable
        Dim listNha As New DataTable()

        Dim para As SqlParameter = New SqlParameter("maquan", SqlDbType.Int)
        para.Direction = ParameterDirection.Input
        para.Value = maquan

        listNha = NhaDAO.TimKiemNhaTheoQuan(para)

        Return listNha
    End Function
    

    Public Shared Function TimKiemNhaTheoLoaiNha(ByVal maloainha As Integer) As DataTable
        Dim listNha As New DataTable()

        Dim para As SqlParameter = New SqlParameter("maloainha", SqlDbType.Int)
        para.Direction = ParameterDirection.Input
        para.Value = maloainha

        listNha = NhaDAO.TimKiemNhaTheoLoaiNha(para)

        Return listNha
    End Function

    Public Shared Function TimKiemNhaTheoTinhTrang(ByVal tinhtrang As Boolean) As DataTable
        Dim listNha As New DataTable()

        Dim para As SqlParameter = New SqlParameter("tinhtrang", SqlDbType.Bit)
        para.Direction = ParameterDirection.Input
        para.Value = tinhtrang

        listNha = NhaDAO.TimKiemNhaTheoTinhTrang(para)

        Return listNha
    End Function

    Public Shared Function LaySLNhaTaiQuan(ByVal maquan As Integer) As Integer
        Dim iSLNhaTaiQuan As Integer

        Dim para As SqlParameter = New SqlParameter("maquan", SqlDbType.Int)
        para.Direction = ParameterDirection.Input
        para.Value = maquan

        iSLNhaTaiQuan = NhaDAO.LaySLNhaTaiQuan(para)

        Return iSLNhaTaiQuan
    End Function

    Public Shared Function KiemTraDayDu(ByVal sonha As String, ByVal duong As String, ByVal gia As String, ByVal dientich As String, ByVal tieude As String)
        Dim kq As Boolean
        If Not sonha = "" Then
            If Not duong = "" Then
                If Not gia = "" Then
                    If Not dientich = "" Then
                        If Not tieude = "" Then
                            kq = True
                        Else
                            kq = False
                        End If
                    Else
                        kq = False
                    End If
                Else
                    kq = False
                End If
            Else
                kq = False
            End If
        Else
            kq = False
        End If
        Return kq
    End Function
End Class
